package com.example.myapplication.ui.tools;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.myapplication.R;
import com.example.myapplication.ui.income.IncomeFragment;

import java.text.NumberFormat;

public class ToolsFragment extends Fragment {

    protected static TextView tvResult = null;
    protected static EditText etStartPoint = null;
    protected static EditText etGongZi = null;
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    private Button calc1, calc2;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tools, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        etStartPoint = getActivity().findViewById(R.id.startpoint);
        etGongZi = getActivity().findViewById(R.id.gongzi);
        tvResult = getActivity().findViewById(R.id.result);
        calc1 = getActivity().findViewById(R.id.calc1);
        calc2 = getActivity().findViewById(R.id.calc2);
        setListeners();
    }

    private class Onclick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.calc1:
                    CalcAfterTaxIncome();
                    break;
                case R.id.calc2:
                    CalcBeforeTaxIncome();
                    break;
            }

        }
    }

    private void setListeners() {
        Onclick onclick = new Onclick();
        calc1.setOnClickListener(onclick);
        calc2.setOnClickListener(onclick);
    }


    public void CalcAfterTaxIncome() {
        double num1 = 0;
        double num2 = 0;
        double num3 = 0;
        double num4 = 0;
        try {
            num4 = new Double(etStartPoint.getText().toString());//(double)etStartPoint.getText().toString();
            num2 = new Double(etGongZi.getText().toString());//(double)etGongZi.getText().toString();
            num3 = num2 - num4;
            if ((num3 >= 100000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += 6000;
                num1 += 7000;
                num1 += 8000;
                num1 += ((num3 - 100000) * 0.45);
            }
            if ((num3 >= 80000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += 6000;
                num1 += 7000;
                num1 += ((num3 - 80000) * 0.4);
            }
            if ((num3 >= 60000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += 6000;
                num1 += ((num3 - 60000) * 0.35);
            }
            if ((num3 >= 40000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += ((num3 - 40000) * 0.3);
            }
            if ((num3 >= 20000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += ((num3 - 20000) * 0.25);
            }
            if ((num3 >= 5000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += ((num3 - 5000) * 0.2);
            }
            if ((num3 >= 2000) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += ((num3 - 2000) * 0.15);
            }
            if ((num3 >= 500) && (num1 == 0)) {
                num1 = 25;
                num1 += ((num3 - 500) * 0.1);
            }
            if ((num3 >= 0) && (num1 == 0)) {
                num1 = num3 * 0.05;
            }
        } finally {
            tvResult.setText("个人所得税: " + nf.format(num1) +
                    " 元\n税后的工资: " + nf.format(num2 - num1) + " 元");
        }
    }

    public void CalcBeforeTaxIncome() {
        double num1 = 0;
        double num2 = 0;
        double num3 = 0;
        double num4 = 0;
        double num5 = 0;
        try {
            num4 = new Double(etStartPoint.getText().toString());//(double)etStartPoint.getText().toString();
            num2 = new Double(etGongZi.getText().toString());//(double)etGongZi.getText().toString();
            num3 = num2 - num4;
            if ((num3 >= 70375) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += 6000;
                num1 += 7000;
                num1 += 8000;
                num5 = (((num1 + num3) - 45000) / 0.55) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 58375) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += 6000;
                num1 += 7000;
                num5 = (((num1 + num3) - 32000) / 0.6) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 45375) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num1 += 6000;
                num5 = (((num1 + num3) - 21000) / 0.65) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 31375) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num1 += 5000;
                num5 = (((num1 + num3) - 12000) / 0.7) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 16375) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num1 += 3000;
                num5 = (((num1 + num3) - 5000) / 0.75) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 4375) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num1 += 450;
                num5 = (((num1 + num3) - 1000) / 0.8) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 1825) && (num1 == 0)) {
                num1 = 25;
                num1 += 150;
                num5 = (((num1 + num3) - 300) / 0.85) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 475) && (num1 == 0)) {
                num1 = 25;
                num5 = (((num1 + num3) - 50) / 0.9) + num4;
                num1 = num5 - num2;
            }
            if ((num3 >= 0) && (num1 == 0)) {
                num5 = (num3 / 0.95) + num4;
                num1 = num5 - num2;
            }
        } finally {
            tvResult.setText(String.format("个人所得税: " + nf.format(num1) +
                    "元\n税前的工资: " + nf.format(num2 + num1) + "  元"));
        }

    }


}